package com.sg.java.apps;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class Test2 {

    public static void main(String[] args) throws Exception {
//        final URL systemResource = ClassLoader.getSystemResource("com/sg/java/apps/");
//        System.out.println(systemResource.toURI());
//        final File dir = new File(systemResource.toURI());
//        Optional.ofNullable(dir.listFiles()).ifPresent(
//                files -> Arrays.stream(files).filter(f -> f.getName().endsWith(".class")).forEach(System.out::println
//                )
//        );
//        System.out.println("20220817".substring(0, 6));
//
//        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
//        final TemporalAccessor parse = yyyyMMdd.parse("20220822");
//        System.out.println(parse.get(ChronoField.DAY_OF_MONTH));
//
//        pointHHmmssToMap().forEach((k, v) -> System.out.println(k + ": " + v));
//        List<String> list = new ArrayList<String>();
//        list.add(null);
//        System.out.println(list.size());
//        List<Integer> list1 = new ArrayList<>(5000);
//        List<Integer> list2 = new ArrayList<>(5000);
//        Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
//        map.put(0, list1);
//        map.put(1, list2);
//        for (int i = 0; i < 5000; i++) {
//            map.get(i % 2).add(i);
//        }

//        StringJoiner s1 = new StringJoiner(",");
//        StringJoiner s2 = new StringJoiner(",");
//        for (int i = 1; i <= 96; i++) {
//            s1.add("u" + i);
//            s2.add("p" + i);
//        }
//        System.out.println(s1);
//        System.out.println(s2);

//        String x = "xxx_20220825";
//        System.out.println(x.substring(x.length() - 8));
//
//        final Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
//        integerStream.forEach(i->{
//            if(i%2==0){
//                throw new RuntimeException();
//            }else{
//                System.out.println(i);
//            }
//        });

        String workDir = System.getProperty("user.dir");
        System.out.println(workDir);
        String jarPath = "C:\\Users\\HUAWEI\\Desktop\\SparkTask\\target\\SparkTask-WordCount.jar";
        JarFile jarFile = new JarFile(jarPath);
        Manifest manifest = jarFile.getManifest();

        System.out.println(Test2.class.getProtectionDomain().getCodeSource().getLocation().toString());

        manifest.getMainAttributes().forEach((k, v) -> System.out.println(k + ":" + v));

        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            String name = jarEntry.getName();
            if (name.contains("kafka-")) {
                InputStream is = jarFile.getInputStream(jarEntry);
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            }

//            else if (name.endsWith(".class")) {
//                String className = name.replaceAll("/", ".");
//                className = className.substring(0, className.length() - 6);
//                System.out.println(className);
//                if (className.contains("YHVolt")) {
//                    final Class<?> aClass = Class.forName(className);
//                    final Class<?> bClass = Class.forName(className);
//
//                    System.out.println("WTF：" + aClass);
//                    InputStream is = jarFile.getInputStream(jarEntry);
//                    InputStreamReader isr = new InputStreamReader(is);
//                    BufferedReader br = new BufferedReader(isr);
//                    String line;
//                    while ((line = br.readLine()) != null) {
//                        System.out.println(line);
//                    }
//                    br.close();
//
//                }
////                jarEntry.getAttributes().forEach((k, v) -> System.out.println(k + ":" + v));
////                System.out.println(name.replaceAll("/","."));
////                Class.forName(name.replaceAll("/","."));
////                break;
//            }
        }

        jarFile.close();
//        File file = new File(jarPath+"!/runData.sh");
//        BufferedReader br = new BufferedReader(new FileReader(file));
//        String line;
//        while ((line = br.readLine()) != null){
//            System.out.println(line);;
//        }
//        br.close();

    }

}
